package com.example.data.network

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit

class TokenManager(context: Context) {
    companion object {
        private const val PREF_NAME = "auth_prefs"
        private const val KEY_TOKEN = "auth_token"
        
        @Volatile
        private var INSTANCE: TokenManager? = null
        
        fun getInstance(context: Context): TokenManager {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: TokenManager(context).also { INSTANCE = it }
            }
        }
    }
    
    private val prefs: SharedPreferences = 
        context.applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
    
    fun saveToken(token: String) {
        prefs.edit {
            putString(KEY_TOKEN, token)
        }
    }
    
    fun getToken(): String? {
        return prefs.getString(KEY_TOKEN, null)
    }
    
    fun clearToken() {
        prefs.edit {
            remove(KEY_TOKEN)
        }
    }
    
    fun hasToken(): Boolean {
        return !getToken().isNullOrEmpty()
    }
} 